home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / system / misc / low_leve / adidrv.taz / adidrv / adidrv.c next >
Encoding:
C/C++ Source or Header  |  1993-06-02  |  4.4 KB  |  218 lines

  1. #if 0
  2. #
  3. # ADIDRV V1.00
  4. # Driver for configuring VL-Bus IDE-Card with Appian ADI2 and Acer 5105 Chip.
  5. # For help call ADIDRV without parameters.
  6. #
  7. # Copyright (C) 1993, by Karsten Ball\"uder, Germany
  8. #            
  9. #        For any comments, questions, etc, send e-mail to:
  10. #                kballued@jupiter.rz.uni-osnabrueck.de
  11. #
  12. # This software is in the public domain. Permission is granted to modify and 
  13. # distribute it free of charge as long as it is accompanied with a note naming
  14. # the original author.
  15. #
  16. # See README for details.
  17. #
  18. # For compiling adidrv.c simply call it as a shell command!
  19. # Shell script for compiling adidrv.c:
  20. gcc -O6 -o adidrv adidrv.c
  21. exit
  22. #endif
  23. /***************** here starts the C-part of it... ***********************/
  24.  
  25. #define USAGE \
  26.         "\nADI2 configuration program (C) 1993 by Karsten Ballueder\n"\
  27.         "----------------------------------------------------------\n"\
  28.         "                     kballued@jupiter.rz.uni-osnabrueck.de\n\n"\
  29.         "USAGE:    %s  -dapwsl [filename] [register:value] [address]\n\n"\
  30.         "\td - detect base adress\t\ta - set base address\n"\
  31.         "\tp - print all registers\t\tw - write register with value\n"\
  32.         "\ts - save all registers to file\tl - load all registers from file\n\n"
  33.  
  34. #include "main.h"
  35.  
  36. #include     <sys/stat.h>
  37. #include     <stdlib.h>
  38. #include    <fcntl.h>
  39. #include    <unistd.h>
  40. #include     <stdio.h>
  41.  
  42. #define     ID    0x004
  43. #define     IDX     0x008
  44. #define     DATA    0x00c
  45. #define     BASE_A    0x0030
  46. #define     BASE_B    0x00b0
  47.  
  48. #define     CHIP_ID    0x33
  49.  
  50. unsigned int     base[] = {BASE_A, BASE_B};
  51. unsigned int     BASE = BASE_B;
  52. int         portfd = -1;
  53.  
  54.  
  55. int access_ports(void)
  56. {
  57.     return open("/dev/port",O_RDWR);
  58. }
  59.  
  60. unsigned int read_adi_port(unsigned idx)
  61. {
  62.     unsigned char     data;
  63.  
  64.     if(idx > 0x0f)
  65.     {    printf("\aError: Cannot access register %02u.\n",idx);
  66.         return;
  67.     }    
  68.     lseek(portfd,BASE+IDX,SEEK_SET);
  69.     data = idx, write(portfd,&data,1);
  70.     
  71.     lseek(portfd,BASE+DATA,SEEK_SET);
  72.     read(portfd,&data,1);    
  73.  
  74.     return data;
  75. }
  76.  
  77. void write_adi_port(unsigned idx, unsigned value)
  78. {
  79.     unsigned char     data;
  80.  
  81.  
  82.     if(idx > 0x0f)
  83.     {    printf("\aError: Cannot access register %02u.\n",idx);
  84.         return;
  85.     }    
  86.     lseek(portfd,BASE+IDX,SEEK_SET);
  87.     data = idx, write(portfd,&data,1);
  88.     
  89.     lseek(portfd,BASE+DATA,SEEK_SET);
  90.     data = value, write(portfd,&data,1);    
  91.     printf("wrote %u to %u\n", value, idx);
  92. }
  93.  
  94. unsigned char get_id(void)
  95. {
  96.     unsigned char data;
  97.     
  98.     lseek(portfd,BASE+ID,SEEK_SET);
  99.     read(portfd,&data,1);
  100.     return data;
  101. }
  102.  
  103. void save_conf(char *file)
  104. {
  105.     int fd = open(file,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR);
  106.     unsigned int i;
  107.     unsigned char data;
  108.  
  109.     if(fd < 1)
  110.     {    printf("\aCannot open file %s for write!\n", file);
  111.         return;
  112.     }
  113.  
  114.     for(i = 1; i <=    0x0F; i++)
  115.     {    data = read_adi_port(i);
  116.         write(fd, &data, sizeof data);
  117.     }
  118.     close(fd);
  119. }
  120.     
  121. void load_conf(char *file)
  122. {
  123.     int fd = open(file,O_RDONLY);
  124.     unsigned int i;
  125.     unsigned char data;
  126.  
  127.     if(fd < 1)
  128.     {    printf("\aCannot open file %s for read!\n", file);
  129.         return;
  130.     }
  131.  
  132.     for(i = 1; i <=    0x0F; i++)
  133.     {    read(fd, &data, sizeof data);
  134.         write_adi_port(i, data);
  135.     }
  136.     close(fd);
  137. }
  138.  
  139. void detect_adi2(void)
  140. {    
  141.     unsigned int     i, flag;
  142.  
  143.     flag = 255;
  144.     for(i = 0; i < 2; i++)
  145.     {    BASE = base[i];
  146.         if(get_id()==0x33)
  147.             printf("ADI2 detected at 0x%02x.\n",
  148.                 BASE), flag = i;
  149.             
  150.     }
  151.     if(flag == 255)
  152.         printf("\aALERT! No ADI2 detected.\n");
  153.     else
  154.         BASE = base[flag];
  155. }
  156.  
  157.  
  158. MAIN
  159. {
  160.     unsigned i, flag;
  161.     char *pname = *argv;
  162.  
  163.     if((portfd = access_ports()) < 0)
  164.         printf("\aFatal error: Cannot open \\dev\\port!\n"),
  165.         exit(1);
  166.  
  167.     if(argc < 2)
  168.         printf(USAGE,pname), exit(EXIT_FAILURE);
  169.  
  170.     write_adi_port(ID,CHIP_ID);
  171.  
  172.     OPT
  173.     ARG 'd':
  174.         detect_adi2();        
  175.     ARG 'a':
  176.         PARM
  177.         if(sscanf(*argv,"%x", &BASE))
  178.             printf("ADI2 base port set to 0x%02x.\n", BASE);
  179.         else
  180.             printf("\aERROR: option -a must be followed by hexadecimal base address!\n");
  181.     NEXTOPT    
  182.     ARG 'p':
  183.         printf("Reg.\tValue\n");        
  184.         for(i=0; i <= 0x0F; i++)
  185.             printf("0x%02x\t0x%02x\n",(unsigned) i, read_adi_port(i));
  186.  
  187.     ARG 's':
  188.         PARM
  189.         if(*argv)
  190.             save_conf(*argv);
  191.         else
  192.             printf("\aERROR: option -s must be followed by filename!\n");
  193.     NEXTOPT
  194.     ARG 'l':
  195.         PARM
  196.         if(*argv)
  197.             load_conf(*argv);
  198.         else
  199.             printf("\aERROR: option -l must be followed by filename!\n");
  200.     NEXTOPT
  201.     ARG 'w':
  202.         PARM
  203.         if(sscanf(*argv,"%u:%u", &i, &flag)==2)
  204.             write_adi_port(i,flag);
  205.         else
  206.             printf("\aError: option -w must be followed by register:value.\n");
  207.     NEXTOPT
  208.     ARG 'h':
  209.         printf(USAGE,pname);
  210.     OTHER 
  211.         printf("\aError: unknown option.\n");
  212.         printf(USAGE,pname);
  213.     ENDOPT
  214.  
  215.     close(portfd);
  216.     return EXIT_SUCCESS;
  217. }
  218.